/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mx.netbrain.dao.impl;

import java.util.List;
import mx.netbrain.dao.StorageDao;
import mx.netbrain.model.Storage;
import mx.netbrain.util.HibernateUtil;
import org.hibernate.Session;

/**
 *
 * @author gamma
 */
public class StorageDaoImpl implements StorageDao {

    @Override
    public void save(Storage storage) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.persist(storage);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al guardar el registro");
            session.beginTransaction().rollback();
        }
    }

    @Override
    public void update(Storage storage) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try{
            session.beginTransaction();
            session.merge(storage);
            session.beginTransaction().commit();
        }catch (Exception e){
            System.out.println();
            session.beginTransaction().rollback();
        }
    }

    @Override
    public Storage findById(Integer id) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return (Storage) session.load(Storage.class, id);
    }

    @Override
    public List<Storage> listAll() {
        Session session = HibernateUtil.getSessionFactory().openSession();
        return session.createQuery("from Storage").list();
    }

    @Override
    public void delete(Storage storage) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        try {
            session.beginTransaction();
            session.delete(storage);
            session.beginTransaction().commit();
        } catch (Exception e) {
            System.out.println("Error al eliminar el registro");
            session.beginTransaction().rollback();
        }
    }
}
